package com.kingbook.services;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kingbook.common.utils.StringUtils;
import com.kingbook.core.base.SupportService;
import com.kingbook.core.result.JSONResult;
import com.kingbook.entity.SmsTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * @Author Bucky
 * @Date 2020-04-26 11:10:45
 * @Version 1.0
 */
@Slf4j
@Service
public class ConfigService extends SupportService {

    @PostConstruct
    private void init() {
        setNamespace("Config");
    }


    public JSONResult queryIntroUrls() {
        JSONObject json = new JSONObject();
        JSONResult result = query("queryIntroUrls", null);
        if (result.isSuccess() && result.getCount() > 0) {
            JSONArray array = result.getData();
            JSONObject jo;
            for (int i = 0; i < array.size(); i++) {
                jo = array.getJSONObject(i);
                json.put(jo.getString("code"), jo.getString("value"));
            }
        }
        return JSONResult.success(json);
    }

    public JSONResult queryOssConfig() {
        JSONObject json = new JSONObject();
        json.put("accessKeyId", "");
        json.put("accessKeySecret", "");
        json.put("policyTimeOut", "");
        JSONResult result = query("queryOssConfig", null);
        if (result.isSuccess() && result.getCount() > 0) {
            JSONArray array = result.getData();
            JSONObject jo;
            for (int i = 0; i < array.size(); i++) {
                jo = array.getJSONObject(i);
                json.put(jo.getString("code"), jo.getString("value"));
            }
        }
        return JSONResult.success(json);
    }

    String queryDomainNameUrl() {
        JSONResult result = query("queryDomainNameUrl", null);
        if (result.isSuccess() && result.getCount() > 0) {
            return StringUtils.trimToEmpty(result.getDataElement("value"));
        }
        return "";
    }

    SmsTemplate querySmsTemplate() {
        SmsTemplate template = new SmsTemplate();
        Map<String, Object>[] result = queryMap("querySmsParam", null);
        for (Map<String, Object> re : result) {
            switch (re.get("code").toString()) {
                case "accessKeyId":
                    template.setAccessKeyId(re.get("value").toString());
                    break;
                case "accessKeySecret":
                    template.setAccessKeySecret(re.get("value").toString());
                    break;
                case "regionId":
                    template.setRegionId(re.get("value").toString());
                    break;
                case "domainUrl":
                    template.setDomainUrl(re.get("value").toString());
                    break;
                case "isRealSend":
                    template.setRealSend("yes".equalsIgnoreCase(re.get("value").toString()));
                    break;
                case "timeout":
                    template.setTimeout(Integer.parseInt(re.get("value").toString()));
            }
        }
        return template;
    }
}
